import { Entity, Column, Index, ManyToOne, JoinColumn } from 'typeorm';
import { BaseEntity } from '../common/base/base.entity';
import { Category } from './category.entity';

@Entity('products')
@Index('idx_status', ['status'])
@Index('idx_is_hot', ['isHot'])
@Index('idx_is_featured', ['isFeatured'])
export class Product extends BaseEntity {
  @Column({
    type: 'varchar',
    length: 200,
    comment: '商品标题',
  })
  title: string;

  @Column({
    type: 'text',
    nullable: true,
    comment: '商品描述',
  })
  description?: string;

  @Column({
    type: 'decimal',
    precision: 10,
    scale: 2,
    comment: '商品价格',
  })
  price: number;

  @Column({
    type: 'decimal',
    precision: 10,
    scale: 2,
    nullable: true,
    comment: '原价',
  })
  originalPrice?: number;

  @Column({
    type: 'decimal',
    precision: 10,
    scale: 2,
    nullable: true,
    comment: 'VIP价格',
  })
  vipPrice?: number;

  @Column({
    type: 'decimal',
    precision: 10,
    scale: 2,
    nullable: true,
    comment: 'SVIP价格',
  })
  svipPrice?: number;

  @Column({
    type: 'decimal',
    precision: 10,
    scale: 2,
    nullable: true,
    comment: '促销价格',
  })
  salePrice?: number;

  @Column({
    type: 'varchar',
    length: 500,
    nullable: true,
    comment: '封面图URL',
  })
  cover?: string;

  @Column({
    type: 'simple-json',
    nullable: true,
    comment: '图片列表',
  })
  images?: string[];

  @Column({
    type: 'varchar',
    length: 500,
    nullable: true,
    comment: '文件URL',
  })
  fileUrl?: string;

  @Column({
    type: 'bigint',
    nullable: true,
    comment: '文件大小（字节）',
  })
  fileSize?: number;

  @Column({
    type: 'simple-json',
    nullable: true,
    comment: '标签列表',
  })
  tags?: string[];

  @Column({
    type: 'enum',
    enum: ['free', 'vip', 'svip'],
    default: 'free',
    comment: '最低会员要求',
  })
  minMembership: 'free' | 'vip' | 'svip';

  @Column({
    type: 'enum',
    enum: ['draft', 'published', 'offline'],
    default: 'draft',
    comment: '商品状态',
  })
  status: 'draft' | 'published' | 'offline';

  @Column({
    type: 'boolean',
    default: false,
    comment: '是否推荐',
  })
  isFeatured: boolean;

  @Column({
    type: 'boolean',
    default: false,
    comment: '是否热门',
  })
  isHot: boolean;

  @Column({
    type: 'int',
    default: 0,
    comment: '浏览量',
  })
  views: number;

  @Column({
    type: 'int',
    default: 0,
    comment: '下载量',
  })
  downloads: number;

  @Column({
    type: 'int',
    default: 0,
    comment: '排序权重',
  })
  sort: number;

  // 分类ID（外键）
  @Column({
    type: 'bigint',
    nullable: true,
    comment: '分类ID',
  })
  categoryId?: string;

  // 关联：多个商品属于一个分类
  @ManyToOne(() => Category, (category) => category.products)
  @JoinColumn({ name: 'categoryId' })
  category?: Category;
}
